package com.smartEdu.controller.web;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.commons.Tag_Tips_web;
import com.smartEdu.commons.constant.JspView;
import com.smartEdu.commons.portion.hw.HworkContext;
import com.smartEdu.commons.resp.RespBody;
import com.smartEdu.po.HomeWorkInfoPo;
import com.smartEdu.po.HomeWorkPo;
import com.smartEdu.service.HomeWorkService;
import com.smartEdu.service.impl.web.HomeWorkServiceImpl;
import com.smartEdu.utils.BeanUtil;
import com.smartEdu.utils.JsonUtil;
import com.smartEdu.vo.web.HomeWorkVo;

@Controller
@RequestMapping(value="/web/")
public class HworkController extends WebBaseController {
	private final String[] homeWorkPart = {"ReleasingHw", "NoReleaseHw", "CompleteHw"};
	
	@Autowired
	@Qualifier(value="homeWorkServiceImplWeb")
	private HomeWorkServiceImpl homeWorkService;
	
	@Autowired
	private HworkContext hworkContext;
	
	@RequestMapping(value="hworkview")
	public ModelAndView Releasing(HttpSession httpsession, Model model, ModelAndView modelAndView) {
		Session sess = new Session();
		try {
			GetUidFromSession(httpsession, model);
			String uid = GetInfoFromModel(model, Tag_Tips_web.LOGINUSER).toString();
			sess.put("uid", uid);
//			sess.put(SYSPARAM, GetInfoFromModel(model,SYSPARAM));
			for (String s : homeWorkPart) {
				hworkContext.GetHwork(sess, s);
				homeWorkService.ShowHomeWork(sess);
				sess.put(s, sess.geto(Tag_Tips.DAODATA));
				System.out.println(JsonUtil.toJson(sess.geto(s)));
			}
			modelAndView.setViewName(JspView.HWORKVIEW.getName());
			AssembleResponeBody(sess, homeWorkPart, modelAndView);
		}
		catch(Exception e) {
			modelAndView.setViewName("redirect:/web/dashboard");
		}
		return modelAndView;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="hometables")
	@ResponseBody
	public List<HomeWorkPo> tables(HttpSession httpsession, Model model) {
		Session sess = new Session();
		List<HomeWorkPo>  vo = null;
		try {
			GetUidFromSession(httpsession, model);
			String uid = GetInfoFromModel(model, Tag_Tips_web.LOGINUSER).toString();
			sess.put("uid", uid);
			hworkContext.GetHwork(sess, "CompleteHw");
			homeWorkService.ShowHomeWork(sess);
			vo = (List<HomeWorkPo>) sess.geto(Tag_Tips.DAODATA);
			System.out.println(JsonUtil.toJson(sess.geto(Tag_Tips.DAODATA)));
		}
		catch(Exception e) {
			LogCvt.error("error",e);
			return null;
		}
		return vo;
	}
	
	@RequestMapping(value="releasehwork")
	public ModelAndView releasehwork(HttpSession httpsession, Model model, ModelAndView modelAndView) {
		Session sess = new Session();
		try {
			GetUidFromSession(httpsession, model);
//			String uid = GetInfoFromModel(model, Tag_Tips_web.LOGINUSER).toString();
//			sess.put("uid", uid);
//			for (String s : homeWorkPart) {
//				hworkContext.GetHwork(sess, s);
//				homeWorkService.ShowHomeWork(sess);
//				sess.put(s, sess.geto(Tag_Tips.DAODATA));
//				System.out.println(JsonUtil.toJson(sess.geto(s)));
//			}
			modelAndView.setViewName(JspView.RELEASEHWORK.getName());
//			AssembleResponeBody(sess, homeWorkPart, modelAndView);
		}
		catch(Exception e) {
			modelAndView.setViewName("redirect:/web/dashboard");
		}
		return modelAndView;
	}
	
	/**
	 * 接收web创建的作业信息
	 * RequestBody 发送对象为json
	 * @throws Exception 
	 */
	@RequestMapping(value="submithw",method=RequestMethod.POST)
	@ResponseBody
	public RespBody newHomeWork(@RequestBody HomeWorkVo vo) throws Exception {
		System.err.println(JsonUtil.toJson(vo));
		HomeWorkInfoPo po = BeanUtil.copyProperties(vo, HomeWorkInfoPo.class);
		System.err.println(JsonUtil.toJson(po));
		RespBody respBody = new RespBody();
		homeWorkService.treatmentNewHomeWork(vo);
		return respBody;
	}
}

